package com.xbh.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbh.common.utils.JwtUtil;
import com.xbh.common.web.core.Result;
import com.xbh.management.model.ManSalary;
import com.xbh.management.model.ManDeal;
import com.xbh.management.service.SalaryService;
import com.xbh.management.service.DealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-15 12:54
 */

@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @Autowired
    private DealService dealService;

    @PostMapping("/page")
    public Result page(@RequestBody JSONObject params){

        ManSalary salary = params.toJavaObject(ManSalary.class);
        int pageNum = params.getInteger("pageNum");
        int pageSize = params.getInteger("pageSize");
        IPage<ManSalary> list = salaryService.list(new Page<ManSalary>(pageNum, pageSize), salary);
        return Result.success(list);

    }

    @PostMapping("/save")
    public Result save(@RequestBody JSONObject params){
        ManSalary salary = params.toJavaObject(ManSalary.class);
        boolean flag = salaryService.saveAccount(salary);
        if(flag){
            return Result.success("保存成功");
        }
        return Result.failed("保存失败");

    }

    @PostMapping("/updateSalary")
    public Result updateAccount(@RequestBody JSONObject params) {
        ManSalary salary = params.toJavaObject(ManSalary.class);
        boolean update = salaryService.updateById(salary);
        if(update){
            return Result.success("修改成功");
        }
        return Result.failed("修改失败");
    }


    @PostMapping("/sendSalary")
    @Transactional(rollbackFor = Exception.class)
    public Result sendSalary(@RequestBody JSONObject params){
        ManSalary salary = params.toJavaObject(ManSalary.class);
        Date date = new Date();
        salary.setSalarySendTime(date);
        boolean flag = salaryService.updateById(salary);
        if(flag){
            ManDeal manDeal = new ManDeal();
            manDeal.setSalaryId(salary.getSalaryId());
            manDeal.setSalaryGradeId(salary.getSalaryGradeId());
            manDeal.setSalaryGradeBalance(params.getBigDecimal("salaryGradeBalance"));
            manDeal.setSalarySendTime(date);
            manDeal.setSalaryName(salary.getSalaryName());
            manDeal.setDealType(1);
            manDeal.setCreatePerson(JwtUtil.getUsername());
            manDeal.setUpdatePerson(JwtUtil.getUsername());
            dealService.save(manDeal);
            return Result.success("修改成功");
        }
        return Result.failed("修改失败");
    }
}
